package com.ruoyi.t.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.t.domain.TOrder;
import com.ruoyi.t.domain.TOrderUser;
import com.ruoyi.t.mapper.TOrderMapper;
import com.ruoyi.t.service.ITOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.t.mapper.TOrderStyleMapper;
import com.ruoyi.t.domain.TOrderStyle;
import com.ruoyi.t.service.ITOrderStyleService;

/**
 * 订单款式Service业务层处理
 *
 * @author YY
 * @date 2025-02-11
 */
@Service
public class TOrderStyleServiceImpl implements ITOrderStyleService
{
    @Autowired
    private TOrderStyleMapper tOrderStyleMapper;

    @Autowired
    private ITOrderService orderService;

    @Autowired
    private TOrderMapper tOrderMapper;

    /**
     * 查询订单款式
     *
     * @param id 订单款式主键
     * @return 订单款式
     */
    @Override
    public TOrderStyle selectTOrderStyleById(Long id)
    {
        return tOrderStyleMapper.selectTOrderStyleById(id);
    }

    /**
     * 查询订单款式列表
     *
     * @param tOrderStyle 订单款式
     * @return 订单款式
     */
    @Override
    public List<TOrderStyle> selectTOrderStyleList(TOrderStyle tOrderStyle)
    {
        return tOrderStyleMapper.selectTOrderStyleList(tOrderStyle);
    }

    /**
     * 新增订单款式
     *
     * @param tOrderStyle 订单款式
     * @return 结果
     */
    @Override
    public int insertTOrderStyle(TOrderStyle tOrderStyle)
    {
        tOrderStyle.setCreateTime(DateUtils.getNowDate());
        return tOrderStyleMapper.insertTOrderStyle(tOrderStyle);
    }

    /**
     * 修改订单款式
     *
     * @param tOrderStyle 订单款式
     * @return 结果
     */
    @Override
    public int updateTOrderStyle(TOrderStyle tOrderStyle)
    {
        tOrderStyle.setUpdateTime(DateUtils.getNowDate());

        TOrder tOrder = tOrderMapper.selectTOrderById(tOrderStyle.getOrderId());

        TOrderStyle style = new TOrderStyle();
        style.setOrderId(tOrder.getId());
        style.setHuidanStatus("1");
        List<TOrderStyle> l1 = tOrderStyleMapper.selectTOrderStyleList(style);
        style.setHuidanStatus(null);
        List<TOrderStyle> l2 = tOrderStyleMapper.selectTOrderStyleList(style);
        if(l1.size()==l2.size()){
            tOrder.setStatus("2");
        }
        tOrderMapper.updateTOrder(tOrder);

        return tOrderStyleMapper.updateTOrderStyle(tOrderStyle);
    }

    /**
     * 批量删除订单款式
     *
     * @param ids 需要删除的订单款式主键
     * @return 结果
     */
    @Override
    public int deleteTOrderStyleByIds(Long[] ids)
    {
        return tOrderStyleMapper.deleteTOrderStyleByIds(ids);
    }

    /**
     * 删除订单款式信息
     *
     * @param id 订单款式主键
     * @return 结果
     */
    @Override
    public int deleteTOrderStyleById(Long id)
    {
        TOrderStyle tOrderStyle = tOrderStyleMapper.selectTOrderStyleById(id);

        tOrderStyleMapper.deleteTOrderStyleById(id);

        TOrderStyle tOrderStyleD = new TOrderStyle();
        tOrderStyleD.setOrderId(tOrderStyle.getOrderId());
        List<TOrderStyle> list = tOrderStyleMapper.selectTOrderStyleList(tOrderStyleD);

        TOrder tOrder = orderService.selectTOrderById(tOrderStyle.getOrderId());
        JSONArray jsonArray = JSONArray.from(list);

        tOrder.setStyleList(jsonArray.toJSONString());

        return orderService.updateTOrder(tOrder);
    }
}
